package mysql

import (
	"common/models"
)

func (m Mysql) GroupAllGroupInfoByUserID(ID string) (res []*models.GroupInfo, count int64, err error) {
	err = m.Db.Table("group_info").Where("owner_id =?", ID).Count(&count).Error
	if count > 0 {
		err = m.Db.Table("group_info").Where("owner_id =?", ID).Count(&count).Find(&res).Error
	}
	return
}
func (m Mysql) GroupCreate(val *models.GroupInfo) (err error) {
	err = m.Db.Table("group_info").Create(&val).Error
	return
}
func (m Mysql) GroupSaveByID(val *models.GroupInfo) (err error) {
	err = m.Db.Table("group_info").Where("id = ?", val.ID).Updates(&val).Error
	return
}
func (m Mysql) GroupDelByID(ID string) (err error) {
	err = m.Db.Table("group_info").Unscoped().Where("id = ?", ID).Delete(&models.GroupInfo{}).Error
	return
}
func (m Mysql) GroupLikeByName(name string) (info []*models.GroupInfo, err error) {
	err = m.Db.Table("group_info").Unscoped().Where("name like ?", LikeType+name+LikeType).Limit(10).Find(&models.GroupInfo{}).Error
	return
}
func (m Mysql) GroupSearchByName(name string) (info *models.GroupInfo, err error) {
	err = m.Db.Table("group_info").Where("name = ?", name).First(&info).Error
	return
}
func (m Mysql) GroupSelectByID(id string) (info *models.GroupInfo, err error) {
	err = m.Db.Table("group_info").Where("id = ?", id).First(&info).Error
	return
}
